/*
 * @Date: 2022-02-25 16:37:23
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2023-11-28 11:32:03
 * @FilePath: \server\system\service\SysService\permission.go
 */

package SysService

import (
	"errors"

	"go-easy-dashboard/system/core/db"
	"go-easy-dashboard/system/core/request"
	"go-easy-dashboard/system/model/CommonModel"
	"go-easy-dashboard/system/model/DbModel"
	"gorm.io/gorm"

	"go-easy-dashboard/system/model/RequestModel"
)

type PermissionService struct{}

//新增
func (PermissionService *PermissionService) Add(info *DbModel.SysPermission) (err error) {
	result := db.Instance().Create(info)
	return result.Error
}

//修改
func (PermissionService *PermissionService) Update(info *DbModel.SysPermission) (err error) {
	var user DbModel.SysPermission
	if err := db.Instance().First(&user, info.Name).Error; err != nil {
		return err
	}
	//忽略字段
	return db.Instance().Omit("CreatedAt", "Password").Save(info).Error
}

//复制
func (PermissionService *PermissionService) Copy(info CommonModel.Id) (err error) {
	var data DbModel.SysPermission
	if err := db.Instance().First(&data, info.Id).Error; err != nil {
		return err
	} else {
		data.Name = ""
		data.Name = data.Name + "_copy"
		result := db.Instance().Create(&data)
		return result.Error
	}

}

//分页列表
func (PermissionService *PermissionService) List(info RequestModel.SysPermissionSearch) (err error, list interface{}, total int64) {

	db := db.Instance().Model(&DbModel.SysPermission{})
	//动态查询条件
	request.GenWhere(info, db)
	var userList []DbModel.SysPermission
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("sort asc").Find(&userList).Error
	return err, userList, total
}

//获取所有tree
func (PermissionService *PermissionService) Tree(info RequestModel.SysPermissionSearch) (list interface{}) {
	db := db.Instance().Model(&DbModel.SysPermission{})
	request.GenWhere(info, db)
	var datalist []DbModel.SysPermission
	db.Order("sort asc").Find(&datalist)
	return PermissionService.BuildTree(datalist, "")
}

//将原始数据创建树结构
func (PermissionService *PermissionService) BuildTree(nodes []DbModel.SysPermission, pid string) []DbModel.SysPermission {
	var result []DbModel.SysPermission
	for _, v := range nodes {
		if v.Parent == pid {
			v.Children = PermissionService.BuildTree(nodes, v.Name)
			result = append(result, v)
		}
	}
	return result
}

//删除
func (PermissionService *PermissionService) Remove(info CommonModel.Names) (err error) {
	//判断是否有children
	var count int64
	db.Instance().Model(&DbModel.SysPermission{}).Where("parent in ?", info.Names).Count(&count)
	if count > 0 {
		return errors.New("child permissions exist")
	}

	return db.Instance().Transaction(func(tx *gorm.DB) error {
		tx.Exec("set autocommit=0")
		// 使用 'tx' 而不是 'db'
		if err := tx.Where("permission_name in ?", info.Names).Delete(&DbModel.SysRolePermission{}).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		// 使用 'tx' 而不是 'db'
		if err := tx.Where("permission_name in ?", info.Names).Delete(&DbModel.SysUserPermission{}).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		if err := tx.Delete(&DbModel.SysPermission{}, info.Names).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		// 返回 nil 提交事务
		return nil
	})
}
